import 'package:flutter/material.dart';
import 'overview_api_provider.dart';
import 'models/detail_model.dart';
import 'dart:async';
import 'detail_all.dart';
import 'models/detail_list.dart';
import 'detail_single.dart';
import 'global/event_bus.dart';


class Platform extends StatefulWidget {
  Platform({Key key}) : super(key: key);

  @override
  _PlatformState createState() {
    return _PlatformState();
  }
}

class _PlatformState extends State<Platform> {
  int platIndex = 0;
  int dateTypeIdx = 1;
  String startDate;
  String endDate;
  OverViewProvider provider = OverViewProvider();
  bool isShowAll = true;
  bool hasError = false;
  bool isLoad = true;
  bool showAll = false;
  SinglePlatDetailModel model;
  DetailListModel listModel;
  List<PlatInfoData> platList;

  @override
  void initState() {
    super.initState();
    platIndex = 0;
    isShowAll = true;
    isLoad = false;
    getPlatList();
  }
  //平台信息列表
  Future<void> getPlatList() async {
    isLoad = true;
    hasError = false;
    setState(() {});
    try {
      platList = await provider.fetchPlatList();
      PlatInfoData allPlatData = PlatInfoData.defaultAll();
      platList = [allPlatData] + platList;
      listModel = await provider.fetchDetailList(dateTypeIdx,startDate,endDate);
      hasError = false;
      isLoad = false;
      setState(() {});
    } catch (e) {
      isLoad = false;
      hasError = true;
      platList = [];
      setState(() {});
    }
  }

  //平台详情列表
  Future<dynamic> getPlatformDetail() async {
    String domain = platList[platIndex].key;
    isLoad = true;
    hasError = false;
    setState(() {});
    if (isShowAll) {
      try {
        listModel = await provider.fetchDetailList(dateTypeIdx, startDate, endDate);
        if(listModel!=null && listModel.resObj!=null && listModel.resObj.length>0) {
          bus.emit('nowdate',listModel.resObj[0].nowDate);
        }
        isLoad = false;
        hasError = false;
        setState(() {});
      } catch (e) {
        setState(() {
          isLoad = false;
          hasError = true;
        });
      }
    } else {
      try {
        model = await provider.fetchPlatDetail(dateTypeIdx,domain);
        isLoad = false;
        hasError = false;
        setState(() {});
      } catch (e) {
        print(e);
        setState(() {
          isLoad = false;
          hasError = true;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        SizedBox(
          width: 120,
          child: makeListView(),
        ),
        Expanded(child: isShowAll ?
        AllDetailTable(
          isLoad: isLoad,
          hasError: hasError,
          datas: listModel,
          onClick: (idx) {
            dateTypeIdx = idx;
            getPlatformDetail();
          },
          onDateChoose: (start, end){
            dateTypeIdx = 0;
            startDate = start;
            endDate = end;
            getPlatformDetail();
          },
        ) :
        SingleDetailTable(
          isLoad: isLoad,
          hasError: hasError,
          filterItems: allIndexes,
          model: model,
          onClick: (idx) {
            dateTypeIdx = idx + 1;
            getPlatformDetail();
          },
        ))
      ],
    );
  }



  ListView makeListView() {
    if (platList == null || platList.length == 0) {
      return null;
    }
    return ListView(
        children: platList.map((e) {
      int idx = platList.indexOf(e);
      bool selected = (idx == platIndex);
      return DecoratedBox(
        child: GestureDetector(
          child: SizedBox(
              height: 50,
              child: Padding(
                padding: const EdgeInsets.all(5),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    idx == 0
                        ? allDetailBtn(e)
                        : Image.network(
                            e.logo,
                            width: 50,
                          ),
                    Text(
                      e.key,
                      style: TextStyle(color: Colors.white),
                    )
                  ],
                ),
              )),
          onTap: () {
            platIndex = idx;
            final curPlat = platList[platIndex];
            if (curPlat.key == '全部') {
              if(!isShowAll){
                dateTypeIdx = 1;
              }
              isShowAll = true;
            } else {
              if(isShowAll){
                dateTypeIdx = 1;
              }
              isShowAll = false;
            }
            getPlatformDetail();
          },
        ),
        decoration: BoxDecoration(
            border: selected
                ? Border(left: BorderSide(width: 2, color: Colors.cyan))
                : null),
      );
    }).toList());
  }

  Widget allDetailBtn(PlatInfoData data) {
    return Center(
        child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Image(
          image: AssetImage(data.logo),
          width: 30,
        ),
        Text(
          data.key,
        ),
        Image(
          image: AssetImage('images/arrow_down.png'),
          width: 10,
        ),
      ],
    ));
  }
}
